<template>
	<xn-form-container
		:title="formData.id ? '编辑库存流水' : '增加库存流水'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="商品ID：" name="streamProductionId">
				<xn-page-select
					ref="xnstreamProductionIdPageSelectRef"
					v-model:value="formData.streamProductionId"
					placeholder="请选择商品ID"
					allow-clear
					show-search
					:page-function="streamProductionIdSelector"
				/>
			</a-form-item>
			<a-form-item label="商品编码：" name="streamProductionCode">
				<a-input v-model:value="formData.streamProductionCode" placeholder="请输入商品编码" allow-clear/>
			</a-form-item>
			<a-form-item label="商品条码：" name="streamProductionBarcode">
				<a-input v-model:value="formData.streamProductionBarcode" placeholder="请输入商品条码" allow-clear/>
			</a-form-item>
			<a-form-item label="商品名称：" name="streamProductionName">
				<a-input v-model:value="formData.streamProductionName" placeholder="请输入商品名称" allow-clear/>
			</a-form-item>
			<a-form-item label="商品规格：" name="streamProductionSpec">
				<a-input v-model:value="formData.streamProductionSpec" placeholder="请输入商品规格" allow-clear/>
			</a-form-item>
			<a-form-item label="商品单位：" name="streamProductionUnit">
				<a-input v-model:value="formData.streamProductionUnit" placeholder="请输入商品单位" allow-clear/>
			</a-form-item>
			<a-form-item label="门店ID：" name="streamStoreId">
				<xn-page-select
					ref="xnstreamStoreIdPageSelectRef"
					v-model:value="formData.streamStoreId"
					placeholder="请选择门店ID"
					allow-clear
					show-search
					:page-function="streamStoreIdSelector"
				/>
			</a-form-item>
			<a-form-item label="仓库ID：" name="streamStorageId">
				<a-input v-model:value="formData.streamStorageId" placeholder="请输入仓库ID" allow-clear/>
			</a-form-item>
			<a-form-item label="库位：" name="streamStoreLocation">
				<a-input v-model:value="formData.streamStoreLocation" placeholder="请输入库位" allow-clear/>
			</a-form-item>
			<a-form-item label="入库数量：" name="streamInNumber">
				<a-input v-model:value="formData.streamInNumber" placeholder="请输入入库数量" allow-clear/>
			</a-form-item>
			<a-form-item label="出库数量：" name="streamOutNumber">
				<a-input v-model:value="formData.streamOutNumber" placeholder="请输入出库数量" allow-clear/>
			</a-form-item>
			<a-form-item label="业务类型：" name="streamBusinessType">
				<a-select v-model:value="formData.streamBusinessType" placeholder="请选择业务类型"
						  :options="streamBusinessTypeOptions"/>
			</a-form-item>
			<a-form-item label="业务单号：" name="streamBusinessCode">
				<a-input v-model:value="formData.streamBusinessCode" placeholder="请输入业务单号" allow-clear/>
			</a-form-item>
			<a-form-item label="实时库存：" name="streamProductionStock">
				<a-input v-model:value="formData.streamProductionStock" placeholder="请输入实时库存" allow-clear/>
			</a-form-item>
			<a-form-item label="业务对象：" name="streamObjectId">
				<xn-page-select
					ref="xnstreamObjectIdPageSelectRef"
					v-model:value="formData.streamObjectId"
					placeholder="请选择业务对象"
					allow-clear
					show-search
					:page-function="streamObjectIdSelector"
				/>
			</a-form-item>
			<a-form-item label="备注：" name="remark">
				<a-input v-model:value="formData.remark" placeholder="请输入备注" allow-clear/>
			</a-form-item>
			<a-form-item label="排序码：" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序码" allow-clear/>
			</a-form-item>
			<a-form-item label="扩展信息：" name="extJson">
				<a-input v-model:value="formData.extJson" placeholder="请输入扩展信息" allow-clear/>
			</a-form-item>
			<a-form-item label="COMPANY_ID：" name="companyId">
				<a-input v-model:value="formData.companyId" placeholder="请输入COMPANY_ID" allow-clear/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierStoresStreamForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import supplierStoresStreamApi from '@/api/supplier/warehouse/supplierStoresStreamApi'
// 抽屉状态
const open = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const xnstreamProductionIdPageSelectRef = ref();
const streamProductionIdSelector = (param) => {
	return supplierProductionsApi.supplierProductionsPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.productionName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
const xnstreamStoreIdPageSelectRef = ref();
const streamStoreIdSelector = (param) => {
	return supplierStoresApi.supplierStoresPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.storeName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}
const streamBusinessTypeOptions = ref([])
const xnstreamObjectIdPageSelectRef = ref();
const streamObjectIdSelector = (param) => {
	return supplierSuppliersApi.supplierSuppliersPage(param).then((data) => {
		if (data.records) {
			data.records = data.records.map((item) => {
				return {
					name: item.supplierName,
					id: item.id,
				};
			});
		}
		return Promise.resolve(data)
	})
}

// 打开抽屉
const onOpen = (record) => {
	open.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
	nextTick(() => {
		xnstreamProductionIdPageSelectRef.value.onPage({current: 1});
	});
	nextTick(() => {
		xnstreamStoreIdPageSelectRef.value.onPage({current: 1});
	});
	streamBusinessTypeOptions.value = tool.dictList('COMMON_STATUS')
	nextTick(() => {
		xnstreamObjectIdPageSelectRef.value.onPage({current: 1});
	});
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	open.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			supplierStoresStreamApi
				.supplierStoresStreamSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
		.catch(() => {
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
